<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <div class="filter-item" style="min-width:270px;margin-left: 20px;">
        <span style="font-size:13px; vertical-align: 4px;">手机号：</span>
        <el-input class="filter-item" style="width: 200px;" placeholder="请输入手机号" v-model="search.phone"></el-input>
      </div>
      <div class="filter-item" style="min-width:270px;margin-left: 20px;">
        <span style="font-size:13px; vertical-align: 4px;">用户名：</span>
        <el-input class="filter-item" style="width: 200px;" placeholder="请输入用户名" v-model="search.userAccount"></el-input>
      </div>
      <div class="filter-item" style="min-width:270px;margin-left: 20px;">
        <span style="font-size:13px; vertical-align: 4px;">姓名：</span>
        <el-input class="filter-item" style="width: 200px;" placeholder="请输入姓名" v-model="search.realName"></el-input>
      </div>
      <div class="filter-item" style="min-width:270px;margin-left: 20px;">
        <span style="font-size:13px;">状态：</span>
        <el-select v-model="search.delTag" placeholder="请选择">
          <el-option label="全部" value=" "></el-option>
          <el-option label="启用" value="0">启用</el-option>
          <el-option label="禁用" value="1">禁用</el-option>
        </el-select>
      </div>
      <el-button class="filter-item" v-waves icon="el-icon-search" @click="handleFilter">搜索</el-button>
    </div>
    <el-table v-loading="!pload" @selection-change="selectionChange" ref="multipleTable" border :data="tableData">
      <el-table-column type="selection" width="55" align="center">
      </el-table-column>
      <el-table-column label="头像" show-overflow-tooltip>
        <template slot-scope="scope">
          <img v-if="/^[0-9]$/.test(scope.row.avatar)" :src="`static/img/avatar${scope.row.avatar}.png`" width="80px" height="80px" alt="">
          <img v-else :src="$S.path + scope.row.avatar" width="80px" height="80px" alt="">
        </template>
      </el-table-column>
      <el-table-column property="userAccount" label="用户名" show-overflow-tooltip>
      </el-table-column>
      <el-table-column property="realName" label="姓名" show-overflow-tooltip>
      </el-table-column>
      <el-table-column property="gender" label="性别" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="手机号" show-overflow-tooltip>
        <template slot-scope="scope">
          <span v-if="permission.operation">{{scope.row.phone}}</span>
        </template>
      </el-table-column>
      <el-table-column property="email" label="身份" show-overflow-tooltip>
        <template slot-scope="scope">
          <span v-if="scope.row.authType === '0'">普通用户</span>
          <span v-if="scope.row.authType === '1'">理投专家</span>
          <span v-if="scope.row.authType === '2'">理投机构</span>
          <span v-if="scope.row.authType === '3'">理投达人</span>
        </template>
      </el-table-column>
      <el-table-column label="注册时间" align="center" width="180" show-overflow-tooltip>
        <template slot-scope="scope">
          {{scope.row.createTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}')}}
        </template>
      </el-table-column>
      <el-table-column property="sourceAddress" label="来源网址" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="状态" align="center" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.delTag === '0'" type="success" size="mini">启用</el-tag>
          <el-tag v-else type="info" size="mini">停用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="250" align="center">
        <template slot-scope="scope">
          <el-button v-if="scope.row.delTag === '1'&&permission.operation" size="mini" type="primary" @click.native="updateStatus(scope.row)" plain>启用</el-button>
          <el-button v-if="scope.row.delTag === '0'&&permission.operation" size="mini" type="primary" @click.native="updateStatus(scope.row)" plain>禁用</el-button>
          <el-button  v-if="permission.modify" size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-size="limit" :page-sizes="limits" background layout="sizes, prev, pager, next, jumper, total" :total="count">
      </el-pagination>
    </div>
    <edit v-if="edit" :data="selectTable" @getData="getData"></edit>
  </div>
</template>

<script>
import * as api from '@/utils/api'
import waves from '@/directive/waves' // 水波纹指令
import edit from './edit'
export default {
  directives: {
    waves
  },
  data() {
    return {
      tableData: [],
      search: {
      },
      permission: {
        modify: null,
        operation: null
      },
      selection: [],
      selectTable: {},
      pload: false,
      edit: false,
      currentPage: 1,
      limit: 20,
      limits: [10, 20, 50, 100],
      count: 0
    }
  },
  components: {
    edit
  },
  created() {
    this.getData()
    this.permission.modify = this.getPermission('sysUser:edit')
    this.permission.operation = this.getPermission('sysUser:operation')
  },
  methods: {
    selectionChange(selection) {
      this.selection = selection
    },
    handleFilter() {
      this.currentPage = 1
      this.getData()
    },
    toggleSelection(s) {
      if (s === -1) {
        this.tableData.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row, true)
        })
      }
      if (s === 0) {
        this.tableData.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      }
      if (s === 1) {
        this.$refs.multipleTable.clearSelection()
      }
    },
    handleEdit(index, row) {
      this.selectTable = JSON.parse(JSON.stringify(row))
      this.selectTable.method = 'edit'
      this.edit = true
    },
    getPermission(func) {
      const menus = this.$store.state.user.menu
      let result = ''

      function dynamicMenu(menus) {
        for (const i in menus) {
          if (menus[i].children.length) {
            dynamicMenu(menus[i].children)
          } else {
            if (menus[i].buttons.length) {
              dynamicMenu(menus[i].buttons)
            }
          }
          if (menus[i].functionCode === func) {
            result = menus[i].functionCode
            return false
          }
        }
      }
      dynamicMenu(menus)
      return result
    },
    async getData() {
      const data = this.JSONo(
        {
          pageNum: this.currentPage,
          pageSize: this.limit
        },
        this.search
      )
      this.pload = false
      const Datas = await api.getucUserList(data)
      this.tableData = Datas.list
      this.count = Datas.total
      this.pload = true
    },
    updateStatus(item) {
      this.$confirm(`确认要${item.delTag !== '0' ? '启用' : '禁用'}吗`, { type: 'warning' }).then(() => {
        api.modifyucUserStatus({
          userId: item.userId,
          delTag: item.delTag === '0' ? '1' : '0'
        })
        this.getData()
      }).catch(() => {
        return false
      })
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.limit = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getData()
    }
  }
}
</script>
